package com.personalblog.service.serviceImpl;

import com.personalblog.mapper.CommentMapper;
import com.personalblog.po.Comment;
import com.personalblog.service.CommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/3/31 - 12:38
 */
@Service
public class CommentServiceImpl implements CommentService {
    private static final Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public int saveComment(Comment comment) {
        ValueOperations<String, Comment> operations = redisTemplate.opsForValue();
        Integer parentCommentId = comment.getParentComment().getCommentId();
        Integer blogId = comment.getBlog().getBlogId();
        if (parentCommentId != -1){
            comment.setParentComment(commentMapper.findCommentByParentCommentId(parentCommentId));

        }else {
            comment.setParentComment(null);
        }
        comment.setCommentCreateTime(new Date());
        System.out.println("CommentServiceImpl处理之后的comment========>"+comment);
        int result = commentMapper.saveComment(comment);
        if (result != 0){
            String key = "blogComment_" + blogId;
            Boolean hasKey = redisTemplate.hasKey(key);
            if (hasKey){
                redisTemplate.delete(key);
                logger.info("在redis中删除" + key + "的缓存");
            }
        }
        return result;
    }


    //存放迭代找出的所有子代的集合
    private List<Comment> tempReplys = new ArrayList<>();

    @Override
    public List<Comment> listCommentByBlogId(int blogId) {
        ValueOperations<String,List<Comment>> operations = redisTemplate.opsForValue();

       String key = "blogComment_" + blogId;
       Boolean hasKey = redisTemplate.hasKey(key);
       if (hasKey){
           List<Comment> redisComments = operations.get(key);
           logger.info("从redis中读取" + key + "的数据");
           return redisComments;
       }else {

           List<Comment> comments1 = commentMapper.findByBlogIdAndParentCommentIdNull(-1, blogId);
           System.out.println("findByBlogIdAndParentCommentNull查询出来的comments===========>"+comments1);
           //查询出来的parent_comment_id映射到了parentComment.commentId属性，将parent_comment_id为null的行提取出来
           List<Comment> comments = new ArrayList<>();
           for (Comment comment : comments1){
               if (comment.getParentComment().getCommentId() == null){
                   comments.add(comment);
               }
           }
           for (Comment comment : comments){
               Integer commentId = comment.getCommentId();
               String parentCommentNickName1 = comment.getCommentNickName();
               List<Comment> childComments = commentMapper.findByParentCommentIdNotNull(commentId);
               //查询出子评论
               combineChildren(childComments, parentCommentNickName1);
               comment.setReplyComments(tempReplys);
               tempReplys = new ArrayList<>();
           }

           operations.set(key, comments);
           logger.info("在redis中添加" + key + "的缓存");
           return comments;
       }


//        return comments;
    }

    private void combineChildren(List<Comment> childComments, String parentCommentNickName1) {
        //判断是否有一级子回复
        if (childComments.size() > 0){
            //循环找出子评论的id
            for (Comment childComment : childComments){
                String parentCommentNickName = childComment.getCommentNickName();
                childComment.setParentCommentNickName(parentCommentNickName1);
                tempReplys.add(childComment);
                Integer childCommentId = childComment.getCommentId();
                //查询二级以及所有子集回复
                recursively(childCommentId,parentCommentNickName);
            }
        }
    }

    private void recursively(Integer childCommentId, String parentCommentNickName1) {
        //根据子一级评论的id找到子二级评论
        List<Comment> replyComments = commentMapper.findByReplayId(childCommentId);

        if (replyComments.size() > 0){
            for (Comment replyComment : replyComments){
                String parentCommentNickName = replyComment.getCommentNickName();
                replyComment.setParentCommentNickName(parentCommentNickName1);
                Integer replyId = replyComment.getCommentId();
                tempReplys.add(replyComment);
                //循环迭代找出子集回复
                recursively(replyId, parentCommentNickName);
            }
        }
    }


}
